import os
import time
from pathlib import PosixPath

from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from chdwlgcsys.settings import BASE_DIR


# csrf 用于form表单中，作用是跨站请求伪造保护。
@csrf_exempt
def uploadFile(request):
    bakString='fail'
    if request.method == "POST":
        file_obj = request.FILES.get('file')
        file_dir = request.POST.get("fileDir")
        time_stamp = request.POST.get("timestamp")
        file_dir = 'chdwlgcsys/web/sample/uploadfile/' + file_dir
        fileAdress = file_dir + time_stamp + str(file_obj.name)
        if os.path.exists(file_dir)==False:
            os.makedirs(file_dir)
        f = open(fileAdress,'wb')
        for chunk in file_obj.chunks():
            f.write(chunk)
        f.close()
        bakString='success'
    return HttpResponse(bakString)